﻿namespace Pap.Web.Modules.Reports.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;

    using Pap.Data;
    using Pap.Web.Utils;

    using Utils;

    /// <summary>
    /// 
    /// </summary>
    public class MonthlyResponsibleReportViewModel : IViewModel
    {
        #region [ Fields ]

        private readonly IMembershipRepository _membershipRepository;

        /// <summary>
        /// 
        /// </summary>
        private readonly IAdministrationRepository _adminRepository;

        /// <summary>
        /// 
        /// </summary>
        private readonly IReportRepository _ReportRepository;

        #endregion Fields

        #region [ Constructors ]

        /// <summary>
        /// Initializes a new instance of the <see cref="MonthlyResponsibleReportViewModel"/> class.
        /// </summary>
        /// <param name="reportRepository">The report repository.</param>
        /// <param name="adminRepository">The admin repository.</param>
        /// <param name="membershipRepository"></param>
        public MonthlyResponsibleReportViewModel(IReportRepository reportRepository, IAdministrationRepository adminRepository, IMembershipRepository membershipRepository)
        {
            _ReportRepository = reportRepository;
            _adminRepository = adminRepository;
            _membershipRepository = membershipRepository;
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Gets all committees.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ICommittee> GetAllCommittees()
        {
            return _adminRepository.GetAllCommittees();
        }

        /// <summary>
        /// Gets all responsible users.
        /// </summary>
        /// <param name="committeeId">The committee ID.</param>
        /// <returns></returns>
        public IEnumerable<IUser> GetAllResponsibleUsers(Guid committeeId)
        {
            return _membershipRepository.FindUsersByPermission(Permissions.ResponsibleWaitingList).
                Where(u => u.UsersCommittees.Any(x => x.CommitteeID == committeeId));
        }

        /// <summary>
        /// Gets the monthly responisble report.
        /// </summary>
        /// <param name="userId">The user ID.</param>
        /// <param name="committeeId">The committee ID.</param>
        /// <param name="fromDate">From date.</param>
        /// <param name="toDate">To date.</param>
        /// <returns></returns>
        public IEnumerable<IVMonthlyResponsibleReport> GetMonthlyResponisbleReport(Guid userId, Guid committeeId, DateTime? fromDate, DateTime? toDate)
        {
            var result = _ReportRepository.FindMonthlyResponsibleReport(u => u.CommitteeID == committeeId
                                                            && fromDate.HasValue ? u.ExecutionDate >= fromDate : true
                                                            && toDate.HasValue ? u.ExecutionDate <= toDate : true
                                                            && userId != Guid.Empty ? u.UserID == userId : true);

            return result.Union(result).Union(result).ToList();
            //DataSet dsResult = new DataSet();
            //DataTable dtResult = new DataTable();

            //dtResult.Columns.Add("BoardNumber", typeof(string));
            //dtResult.Columns.Add("LoanNumber", typeof(string));
            //dtResult.Columns.Add("CustomerName", typeof(string));
            //dtResult.Columns.Add("Gender", typeof(int));
            //dtResult.Columns.Add("LoanTypeName", typeof(string));
            //dtResult.Columns.Add("Stage", typeof(int));
            //dtResult.Columns.Add("LoanAmount", typeof(double));

            //dtResult.Columns.Add("TotalLoanAmount", typeof(double));
            //dtResult.Columns.Add("CheckDate", typeof(DateTime));
            //dtResult.Columns.Add("CheckNumber", typeof(string));
            //dtResult.Columns.Add("LoanPaymentMethodName", typeof(string));
            //dtResult.Columns.Add("CommitteeName", typeof(string));
            //dtResult.Columns.Add("CommitteeID", typeof(Guid));
            //dtResult.Columns.Add("IsLoanProjectExist", typeof(bool));

            //dtResult.Columns.Add("InstallmentsCount", typeof(int));
            //dtResult.Columns.Add("ExecutionDate", typeof(DateTime));
            //dtResult.Columns.Add("UserID", typeof(Guid));
            //dtResult.Columns.Add("Profit", typeof(double));

            //DataRow dr = null;

            
            //FromDate.HasValue ? u.ExecutionDate >= FromDate : true
            //&& ToDate.HasValue ? u.ExecutionDate <= ToDate : true).ToList();
            //&& UserID != Guid.Empty? u.UserID == UserID : true

            //foreach (VMonthlyResponsibleReport MonthlyResponsibleReport in lstMonthlyResponsibleReport)
            //{
            //    dr = dtResult.NewRow();

            //    dr["BoardNumber"] = MonthlyResponsibleReport.BoardNumber;
            //    dr["LoanNumber"] = MonthlyResponsibleReport.LoanNumber;
            //    dr["CustomerName"] = MonthlyResponsibleReport.CustomerName;
            //    dr["Gender"] = MonthlyResponsibleReport.Gender;
            //    dr["LoanTypeName"] = MonthlyResponsibleReport.LoanTypeName;
            //    dr["Stage"] = MonthlyResponsibleReport.Stage;
            //    dr["LoanAmount"] = MonthlyResponsibleReport.LoanAmount;
            //    dr["TotalLoanAmount"] = MonthlyResponsibleReport.TotalLoanAmount;

            //    if (MonthlyResponsibleReport.CheckDate.HasValue)
            //        dr["CheckDate"] = MonthlyResponsibleReport.CheckDate;
            //    else
            //        dr["CheckDate"] = DBNull.Value;

            //    dr["CheckNumber"] = MonthlyResponsibleReport.CheckNumber;
            //    dr["LoanPaymentMethodName"] = MonthlyResponsibleReport.LoanPaymentMethodName;
            //    dr["CommitteeName"] = MonthlyResponsibleReport.CommitteeName;
            //    dr["CommitteeID"] = MonthlyResponsibleReport.CommitteeID;
            //    dr["IsLoanProjectExist"] = MonthlyResponsibleReport.IsLoanProjectExist;
            //    dr["InstallmentsCount"] = MonthlyResponsibleReport.InstallmentsCount;
            //    dr["ExecutionDate"] = MonthlyResponsibleReport.ExecutionDate;
            //    dr["UserID"] = MonthlyResponsibleReport.UserID;
            //    dr["Profit"] = MonthlyResponsibleReport.Profit;

            //    dtResult.Rows.Add(dr);
            //}

            //dsResult.Tables.Add(dtResult);

            //return dsResult;
        }

        #endregion Methods
    }
}